<?php
/**
**2015-09-06 对接驴妈妈系统 订单信息接收
**User By  PZP
**/
include('/var/www/html/new/d/module/api/LvMaMa/LvMaMa.class.php');
include('/var/www/html/new/d/module/api/function.class.php');

if (!class_exists("go_sql")){
	include("/var/www/html/new/conf/le.je");
	$le=new go_sql();
	$le->connect();
}

$Inside= new SoapClient(null,array(
        "location" => "http://localhost/open/openService/pft_insideMX.php",
        "uri" => "www.16u.com?ac_16u=16ucom|pw_16u=c33367701511b4f6020ec61ded352059|auth_16u=true")
);


$txt=json_encode($_REQUEST);
$lmm = new LvMaMa();
$lmm->logfile($txt,'LvMaMa_pushOrderInfo_data');
/*
$order = <<<xml
<?xml version="1.0" encoding="utf-8"?>
<request>
	<header>
		<signed>ca6e53a2a19e6704f9c870579c390066</signed>
		</header>
	<body>
		<order>
			<orderId>null</orderId>
			<status>NORMAL</status>
			<approveStatus>false</approveStatus>
			<paymentStatus>UNPAY</paymentStatus>
			<credenctStatus>CREDENCE_NO_SEND</credenctStatus>
			<waitPayment></waitPayment>
			<performStatus>UNUSE</performStatus>
		</order>
	</body>
</request>
xml;

{"order":"<?xml version=\"1.0\" encoding=\"utf-8\" ?><request><header><signed>89bf061af0da46aae277d628293d99d3<\/signed><\/header><body><order><orderId>24256050<\/orderId><status>CANCEL<\/status><approveStatus>true<\/approveStatus><paymentStatus>PAYED<\/paymentStatus><credenctStatus>CREDENCE_NO_SEND<\/credenctStatus><waitPayment><\/waitPayment><performStatus>UNUSE<\/performStatus><\/order><\/body><\/request>"}
*/
if(empty($_REQUEST['order'])){
	$code = 1001; 
	$message = "没接收任何数据";
	$response = response($code,$message);
	echo $response;
	exit;
}

$order = $_REQUEST['order'];
$orderxml = simplexml_load_string($order);

$lmmOrder = (string)$orderxml->body->order->orderId;
$status  = (string)$orderxml->body->order->status;
$performStatus = (string)$orderxml->body->order->performStatus; //凭证使用状态 USED已使用，UNUSE未使用
$additional = (string)$orderxml->body->order->credentials->credential->additional; //凭证码

if($lmmOrder == "null"){
	$code = 1011; 
	$message = "没有此订单，请检查订单号是否正确！";
	$response = response($code,$message);
	echo $response;
	exit;
}
if($status == 'NORMAL'){
	if($performStatus == 'USED'){
		$PFT_infunction = new AllFunction();
		
		$str="select pftOrder from all_api_order  where apiOrder='$lmmOrder' limit 1";
		$GLOBALS['le']->query($str);
		$GLOBALS['le']->fetch_assoc();
		$pftOrder  = $GLOBALS['le']->f('pftOrder');
		
		$pft_order_xml = $PFT_infunction->Select_PFTorder($pftOrder);
		$chkIns = 499;
		if((string)$pft_order_xml->Rec->UUstatus==2) $chkIns = 498;		
		$UUordernum = (string)$pft_order_xml->Rec->UUordernum;
		$UUsalerid = (string)$pft_order_xml->Rec->UUsalerid;
		$UUcode = (string)$pft_order_xml->Rec->UUcode;
		$terminal = $PFT_infunction->Unified_Check_IN($UUsalerid,$UUcode,date('Y-m-d H:i:s'),$chkIns);
		if($terminal == 100){
			$lmm->logfile($pftOrder.'|'.$lmmOrder.'|'.$terminal,'lmmterminalCheck_success');
			$selorder = "update all_api_order  set oStatus=1 where pftOrder='$UUordernum' limit 1";
			$GLOBALS['le']->query($selorder);
			$code = 1000; 
			$message = "更新订单状态成功";
			$response = response($code,$message);
			echo $response;
			exit;
		}	
		else{
			$code = $terminal; 
			$message = "更新订单状态失败";
			$lmm->logfile($pftOrder.'|'.$lmmOrder.'|'.$terminal,'terminalCheck_fail');	
			$response = response($code,$message);
			echo $response;
			exit;
		}
	}
	elseif($performStatus == 'UNUSE'){
		if($additional != 'null'){
			$updateorder = "update all_api_order set apiCode='$additional' where coopB=1 and apiOrder='$lmmOrder' limit 1";
			$GLOBALS['le']->query($updateorder);
		}
		$code = 1000; 
		$message = "接收成功，更新第三方凭证码";
		$response = response($code,$message);
		echo $response;
		exit;
	}			
}elseif($status == 'CANCEL'){
	$updateorder = "update all_api_order set oStatus=3,handleStatus=0 where coopB=1 and apiOrder='$lmmOrder' limit 1";
	$GLOBALS['le']->query($updateorder);
	$lmm->logfile($lmmOrder,'lvmama_cancel_success');
	//收到退单成功后取消票付通订单
	$selorder = "select pftOrder from all_api_order where apiOrder='$lmmOrder' limit 1";
	$GLOBALS['le']->query($selorder);
	$GLOBALS['le']->fetch_assoc();
	$pftOrder = $GLOBALS['le']->f('pftOrder');
	$Cancel = $Inside->Order_Change_Pro($pftOrder,0,0,0,0,"","","");
	$lmm->logfile($pftOrder.'|'.$Cancel,'lvmamam_cancel_order');
	if($Cancel == 100){
		$code = 1000; 
		$message = "退款成功";
		$response = response($code,$message);
		echo $response;
		exit;
	}
}elseif($status== 'FINISHED' || $status == 'REFUND'){
	$code = 1000; 
	$message = "接收成功，不处理任何动作";
	$response = response($code,$message);
	echo $response;
	exit;
}


function response($code,$message){
	$return =<<<xml
	<?xml version="1.0" encoding="utf-8"?>
		<response>
			<state>
				<code>{$code}</code>
				<message>{$message}</message>
			</state>
		</response>
xml;
	return $return;
}
?>
SHED' || $status == 'REFUND'){
	$code = 1000; 
	$message = "接收成功，不处理任何动作";
	$response = response($code,$message);
	echo $response;
	exit;
}


function response($code,$message){
	$return =<<<xml
	<?xml version="1.0" encoding="utf-8"?>
		<response>
			<state>
				<code>{$code}</code>
				<message>{$message}</message>
			</state>
		</response>
xml;
	return $return;
}
?>
